#!/usr/bin/env python

import sys
import getopt

import parser
import drawer
from options import Options

VERSION = "1.0"

__all__ = [ 'VERSION' ]
__author__ = "Giuliani Vito Ivan"
__version__ = VERSION
__license__ = "GPL 2"

def main(argv):
    if not argv:
        argv = sys.argv
    
    try:
        opts, args = getopt.getopt(argv[1:], "hi:o:",
                [   "help", "input=", "output=", "width=", "height=", "bg=",
                    "bg-namespace=", "bg-class=", "bg-func=",
                    "fg-namespace=", "fg-class=", "fg-func="])
    except getopt.GetoptError, err:
        print str(err)
        sys.exit(2)

    input           = None
    output          = 'output.svg'
    width           = 500
    height          = 2000
    step            = 30
    bg              = '#FFFFFF'
    bg_namespace    = '#000000'
    bg_class        = '#777777'
    bg_func         = '#CCCCCC'
    fg_namespace    = '#EEEEEE'
    fg_class        = '#EEEEEE'
    fg_func         = '#000000'

    for option, arg in opts:
        if option == "-h":
            usage(argv)
            sys.exit(1)
        elif option in ["-i", "--input"]:
            input = arg
        elif option in ["-o", "--output"]:
            output = arg
        elif option in ["-w", "--width"]:
            width = arg
        elif option in ["-h", "--height"]:
            height = arg
        elif option == "--bg":
            bg = arg
        elif option == "--bg-namespace":
            bg_namespace = arg
        elif option == "--bg-class":
            bg_class = arg
        elif option == "--bg-func":
            bg_func = arg
        elif option == "--fg-namespace":
            fg_namespace = arg
        elif option == "--fg-class":
            fg_class = arg
        elif option == "--fg-func":
            fg_func = arg
        else:
            print "Invalid option " + option
            usage(argv)
            sys.exit(2)

    if not input:
        print "Please specify an input file"
        usage(argv)
        sys.exit(2)

    options = Options()
    options['input']        = input
    options['output']       = output
    options['width']        = width
    options['height']       = height
    options['step']         = step
    options['bg']           = bg
    options['bg_namespace'] = bg_namespace
    options['bg_class']     = bg_class
    options['bg_func']      = bg_func
    options['fg_namespace'] = fg_namespace
    options['fg_class']     = fg_class
    options['fg_func']      = fg_func

    if not options.isValid():
        usage(argv)
        sys.exit(2)

    parseInput = parser.Parser(input)
    drawer.Drawer(parseInput.parse(), options).draw()

def usage(argv):
    print "Generates an SVG toc-like file starting from a tree map\n"
    print "Usage: " + argv[0]
    print "\t-i    [--input=]    input file"
    print "\t-o    [--output=]   output file (default: output.svg)"
    print "\t--width             width of the output file (default: 500px)"
    print "\t--height            height of the output file (default: 2000px)"
    print "\t--bg                background page color (default: #FFFFFF)"
    print "\t--bg-namespace      namespace background color (default: #000000)"
    print "\t--fg-namespace      namespace foreground color (default: #EEEEEE)"
    print "\t--bg-class          class background color (default: #777777)"
    print "\t--fg-class          class foreground color (default: #EEEEEE)"
    print "\t--bg-func           func background color (default: #CCCCCC)"
    print "\t--fg-func           func foreground color (default: #000000)"

if __name__ == '__main__':
    main(sys.argv)
